package com.shsoft.myThread;

import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.common.Constant;

/**
 * 混色-跑马灯 子线程
 */
public class MixHorseThread extends Thread{
    private MainActivity activity;
    private int bright; // 记录亮度
    private int direction; // 方向
    private int speed; // 移动速度


    public MixHorseThread(MainActivity activity) {
        this.activity = activity;
        bright = Integer.parseInt(activity.horse_bright.getText().toString());
        if(bright * 2 > 255){
            bright = 255;
        }else {
            bright = bright * 2;
        }
        direction = activity.horse_direction_value;
        speed = Integer.parseInt(activity.horse_speed.getText().toString()) * 10;
    }

    @Override
    public void run() {
        System.out.println("混色跑马灯子线程运行");
        while (activity.isPlaying){
            // 每次把所有的颜色按顺时针或者逆时针变换
            if(direction == Constant.CLOCKWISE){ // 顺时针
                int[] rgb = activity.lights_4display.get(Constant.LIGHT_COUNT - 1);
                for(int i = (Constant.LIGHT_COUNT - 1); i > 0 ; i--){
                    activity.lights_4display.put(i,activity.lights_4display.get(i - 1));
                }
                activity.lights_4display.put(0,rgb);
            }else { // 逆时针
                int[] rgb = activity.lights_4display.get(0);
                for(int i = 0; i < (Constant.LIGHT_COUNT - 1) ; i++){
                    activity.lights_4display.put(i,activity.lights_4display.get(i + 1));
                }
                activity.lights_4display.put((Constant.LIGHT_COUNT - 1) ,rgb);
            }
            // 让主线程刷新灯珠区域
            activity.alpha = bright;
            activity.refreshLightsDiv();
            try {
                sleep(speed);
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }
    }
}
